using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SheLife.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class MenstrualNoteController : ControllerBase
    {
        private readonly IMenstrualNoteService _menstrualNoteService;

        public MenstrualNoteController(IMenstrualNoteService menstrualNoteService)
        {
            _menstrualNoteService = menstrualNoteService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<MenstrualNote>> GetMenstrualNote(int id)
        {
            var note = await _menstrualNoteService.GetMenstrualNoteByIdAsync(id);
            if (note == null)
            {
                return NotFound();
            }
            return note;
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<IEnumerable<MenstrualNote>>> GetMenstrualNotesByUserId(int userId)
        {
            var notes = await _menstrualNoteService.GetMenstrualNotesByUserIdAsync(userId);
            return Ok(notes);
        }

        [HttpGet("user/{userId}/daterange")]
        public async Task<ActionResult<IEnumerable<MenstrualNote>>> GetMenstrualNotesByDateRange(
            int userId, 
            [FromQuery] DateTime startDate, 
            [FromQuery] DateTime endDate)
        {
            var notes = await _menstrualNoteService.GetMenstrualNotesByDateRangeAsync(userId, startDate, endDate);
            return Ok(notes);
        }

        [HttpGet("user/{userId}/cycle/{cycleId}")]
        public async Task<ActionResult<IEnumerable<MenstrualNote>>> GetMenstrualNotesByCycleId(int userId, int cycleId)
        {
            var notes = await _menstrualNoteService.GetMenstrualNotesByCycleIdAsync(userId, cycleId);
            return Ok(notes);
        }

        [HttpPost]
        public async Task<ActionResult<MenstrualNote>> CreateMenstrualNote(MenstrualNote note)
        {
            var createdNote = await _menstrualNoteService.CreateMenstrualNoteAsync(note);
            return CreatedAtAction(nameof(GetMenstrualNote), new { id = createdNote.Id }, createdNote);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateMenstrualNote(int id, MenstrualNote note)
        {
            if (id != note.Id)
            {
                return BadRequest();
            }

            await _menstrualNoteService.UpdateMenstrualNoteAsync(note);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteMenstrualNote(int id)
        {
            await _menstrualNoteService.DeleteMenstrualNoteAsync(id);
            return NoContent();
        }
    }
}